//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Galleries.Components;

namespace CommunityServer.Galleries.Controls
{
	/// <summary>
	/// Summary description for PortalPictureListing.
	/// </summary>
	public class AggregatePortalPictureListing : GalleryTemplatedWebControl
	{
		
		#region Public Properties

        [DefaultValue(true)]
        private bool enableTitle = true;

        public bool EnableTitle
        {
            get { return enableTitle; }
            set { enableTitle = value; }
        }


		[DefaultValue( null )]
		private string applicationKey = null;
		public string ApplicationKey
		{
			get { return applicationKey; }
			set { applicationKey = value; }
		}

		[DefaultValue( -1 )]
		private int categoryID = -1;
		public int CategoryID
		{
			get { return categoryID; }
			set { categoryID = value; }
		}

		[DefaultValue( -1 )]
		private int userID = -1;
		public int UserID
		{
			get { return userID; }
			set { userID = value; }
		}

		[DefaultValue( 3 )]
		private int count = 3;
		public int Count
		{
			get { return count; }
			set { count = value; }
		}

		[DefaultValue( GalleryThreadSortBy.ThreadDate )]
		private GalleryThreadSortBy sortBy = GalleryThreadSortBy.ThreadDate;
		public GalleryThreadSortBy SortBy
		{
			get { return sortBy; }
			set { sortBy = value; }
		}

		private SortOrder sortOrder = SortOrder.Descending;
		public SortOrder SortOrder
		{
			get { return sortOrder; }
			set { sortOrder = value; }
		}

		[DefaultValue( true )]
		private bool showPictures = true;
		public bool ShowPictures
		{
			get { return showPictures; }
			set { showPictures = value; }
		}

        private GalleryImageType imageType = GalleryImageType.Thumbnail;
        public GalleryImageType ImageType
        {
            get { return imageType; }
            set { imageType = value; }
        }

		#endregion

		#region Child Controls

		private IText sectionTitle;
		private RepeaterPlusNone pictureList;

		#endregion

		#region Skin

		protected override void AttachChildControls()
		{
			sectionTitle = FindText( "SectionTitle" );
			pictureList = (RepeaterPlusNone)FindControl( "Pictures" );
			InitializeChildControls();

            if(EnableTitle)
            {
                Head.AddSiteNameTitle("Photo Galleries",Context);
                UsersOnline.SetLocation("Photo Galleries");
            }
		}

		private void InitializeChildControls()
		{
			pictureList.ItemDataBound += new RepeaterItemEventHandler(pictureList_ItemDataBound);
			pictureList.NoneItemsDataBound += new RepeaterItemEventHandler(pictureList_NoneItemsDataBound);
		}

		#endregion

		#region Event Overrides

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad (e);
			//if(!Page.IsPostBack)	// JAL -- prevent images from disappearing on postback
				DataBind();
		}

		public override void DataBind()
		{
			base.DataBind();
			BindData();
		}

		#endregion

		private void BindData()
		{
			// Set the area title
			if(sectionTitle != null)
			{
				switch(sortBy)
				{
					case GalleryThreadSortBy.ThreadDate:
						sectionTitle.Text = ResourceManager.GetString( "Gallery_PortalPictureListing_TitlePostDate" );
						break;
					case GalleryThreadSortBy.Rating:
						sectionTitle.Text = ResourceManager.GetString( "Gallery_PortalPictureListing_TitleRating" );
						break;
					case GalleryThreadSortBy.Comments:
						sectionTitle.Text = ResourceManager.GetString( "Gallery_PortalPictureListing_TitleReplies" );
						break;
					case GalleryThreadSortBy.Views:
						sectionTitle.Text = ResourceManager.GetString( "Gallery_PortalPictureListing_TitleViews" );
						break;
				}
			}
			// Setup the query
			GalleryThreadQuery query = new GalleryThreadQuery();

			if(applicationKey != null)
				query.SectionID = Galleries.GetGallery(applicationKey).SectionID;
			else
			query.FilterByList = Galleries.GetGalleries();
			query.CategoryID = categoryID;
			query.UserID = userID;
			query.PageSize = count;
			query.SortBy = sortBy;
			query.SortOrder = sortOrder;
			query.ApplicationPostType = GalleryPostType.Image;
			query.PublishedFilter = GalleryPostPublishedFilter.Published;
			
			pictureList.DataSource = GalleryPosts.GetPictures(query, true, true).Threads;
			pictureList.DataBind();
		}

		private void pictureList_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			GalleryPost dataItem = e.Item.DataItem as GalleryPost;

			switch( e.Item.ItemType ) 
			{
				case ListItemType.Item:
				case ListItemType.AlternatingItem:

					GalleryImage pictureThumb = e.Item.FindControl("PictureThumb") as GalleryImage;
					HyperLink PictureNameLink = e.Item.FindControl( "PictureName" ) as HyperLink;
					Literal fieldName = e.Item.FindControl( "SortedName") as Literal;
					Literal field = e.Item.FindControl( "SortedField") as Literal;

					if(pictureThumb != null)
					{
						pictureThumb.Visible = showPictures;
						if(pictureThumb.Visible == true)
						{
                            pictureThumb.ImageType = this.ImageType;
							pictureThumb.PostID = dataItem.PostID;
							pictureThumb.NavigateUrl = GalleryUrls.Instance().ViewPicture(dataItem.Section.ApplicationKey, CategoryID, dataItem);
							pictureThumb.GalleryPost = dataItem;
							pictureThumb.DataBind();
						}
					}

					if(PictureNameLink != null)
					{
						if(dataItem.Subject != "")
						{
							dataItem.Subject = Globals.HtmlDecode( dataItem.Subject );
							if(dataItem.Subject.Length > 22)
								PictureNameLink.Text = dataItem.Subject.Substring(0, 22) + "...";
							else
								PictureNameLink.Text = dataItem.Subject;
						}
						else
							PictureNameLink.Text = ResourceManager.GetString( "Gallery_NoTitle" );
						PictureNameLink.NavigateUrl = GalleryUrls.Instance().ViewPicture(dataItem.Section.ApplicationKey, CategoryID, dataItem);
					}

				switch(sortBy)
				{
					case GalleryThreadSortBy.ThreadDate:
						CSContext cSContext = CSContext.Current;

						User user = cSContext.User;
						DateTime postDate = dataItem.PostDate;
						string dateFormat;

						if(!user.IsAnonymous)
						{
							postDate = user.GetTimezone(dataItem.PostDate);
							dateFormat = user.Profile.DateFormat;
						}
						else
							dateFormat = cSContext.SiteSettings.DateFormat;

						if(fieldName != null)
							fieldName.Text = ResourceManager.GetString( "Gallery_PortalPictureListing_FieldPostDate" );
						if(field != null)
							field.Text = postDate.ToString(dateFormat) + " " + postDate.ToString(cSContext.SiteSettings.TimeFormat);
						break;
					case GalleryThreadSortBy.Rating:
						if(fieldName != null)
							fieldName.Text = ResourceManager.GetString( "Gallery_PortalPictureListing_FieldRating" );
						if(field != null)
						{
							field.Text = dataItem.RatingAverage.ToString("0.0#");
						}
						break;
					case GalleryThreadSortBy.Comments:
						if(fieldName != null)
							fieldName.Text = ResourceManager.GetString( "Gallery_PortalPictureListing_FieldReplies" );
						if(field != null)
							field.Text = dataItem.Replies.ToString();
						break;
					case GalleryThreadSortBy.Views:
						if(fieldName != null)
							fieldName.Text = ResourceManager.GetString( "Gallery_PortalPictureListing_FieldViews" );
						if(field != null)
							field.Text = dataItem.Views.ToString();
						break;
				}

					break;
			}
		}

		private void pictureList_NoneItemsDataBound(object sender, RepeaterItemEventArgs e)
		{

		}
	}
}
